package com.modifiertech.lineage.base;

import com.modifiertech.lineage.BaseConst;
import com.modifiertech.lineage.Lineage;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.commands.LineageCmd;
import com.modifiertech.lineage.effect.EffectRegister;
import com.modifiertech.lineage.event.PlayerDataHandler;
import com.modifiertech.lineage.handler.PlayerManaModifyHandler;
import com.modifiertech.lineage.network.*;
import com.modifiertech.lineage.skills.RangeHealth;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class CommonProxy {

    public void preInit(FMLPreInitializationEvent event){
        //效果注册器预初始化
        EffectRegister.preInit(event);
        //注册
        MinecraftForge.EVENT_BUS.register(PlayerDataHandler.EventHandler.class);
        //网络包
        NetworkHandler.registerMessages(BaseConst.MODID);
        //配置
        ConfigHandler.init(event.getSuggestedConfigurationFile());
        //
    }

    public void init(FMLInitializationEvent event){
        if (ConfigHandler.config.hasChanged()) {
            ConfigHandler.config.save();
        }
    }

    public void postInit(FMLPostInitializationEvent event){

    }

    public EntityPlayer getClientPlayer() {
        return null;
    }

    public String getLocalizedString(String string) {
        return I18n.format(string);
    }

    public void serverStarting(FMLServerStartingEvent event) {
        //cmd
        event.registerServerCommand(new LineageCmd());
        Lineage.logger.info("服务器lineage 指令注册成功.");
    }

    public void registerFakePlayer(){}

    /**执行客户端消息
     * common端不做处理
     */
    public void handleMessageClientSay(MessageClientSay messageClientSay,MessageContext messageContext){
    }
    public void handleMessageClientSkillJumpSplash(MessageClientSkillJumpSplash messageClientSay, MessageContext messageContext){
    }
    public void handleRenderWorldLastEventRangeHealth(float partialTicks){}
    public void handleMessageClientSkillCD(MessageClientSkillCD messageClientSkillCD, MessageContext messageContext){}
    public void handleMessageClientSkillHandingState(MessageClientSkillHandingState messageClientSkillHandingState, MessageContext messageContext){}
    public void handleMessageClientSkillRange(MessageClientSkillRange messageClientSkillRange, MessageContext messageContext){}

    /**从消息中获取状态信息,变更服务端playerData 数据
     *
     * @param messageServerSkillRange
     * @param messageContext
     * @param callback
     */
    public void handleMessageServerSkillRange(MessageServerSkillRange messageServerSkillRange, MessageContext messageContext, Supplier callback){

            EntityPlayer entityPlayer = messageContext.getServerHandler().player;

            PlayerData playerData = PlayerDataHandler.get(entityPlayer);
            if (messageServerSkillRange.getNbtTagCompound().hasKey("state")){
                playerData.updateSkillRangeAvailableStatus(messageServerSkillRange
                        .getNbtTagCompound().getBoolean("state"));
            }
            if (messageServerSkillRange.getNbtTagCompound().hasKey("AlignedBBs")){
                List<AxisAlignedBB> axisAlignedBBS = MessageClientSkillRange.readAABBRange(messageServerSkillRange
                        .getNbtTagCompound());
                playerData.updateSkillRangeAABBList(axisAlignedBBS);
            }
            callback.get();
    }
    public void handleOnPlayerSkillHandingOffRangeHealth(EntityPlayer entityPlayer, float health, Random random){
        EntityPlayerMP entityPlayerMP = (EntityPlayerMP) entityPlayer;
        PlayerData playerData = PlayerDataHandler.get(entityPlayerMP);
        if(playerData.getSkillRangeAvailableStatus()) {
            //选定目标位置所框选实体
            Set<Entity> entities = playerData.getEntityFromAABBSkillRange(entityPlayerMP.getServerWorld());
            for(Entity entity:entities) {
                //除怪物以外均可回血
                if(!(entity instanceof IMob)) {
                    if (entity instanceof EntityLivingBase) {
                        EntityLivingBase livingEntity = (EntityLivingBase) entity;
                        livingEntity.heal(health);//恢复一定血量.
                    }

                }
            }
            //在作用区内生成爱心粒子
            AxisAlignedBB axisAlignedBB = playerData.getCachedAabbSkillRangeList().get(0);
            for (int i = 0; i < 7; i++) {
                // 在 AABB 盒子内随机生成 X, Y, Z 坐标
                double posX = axisAlignedBB.minX + random.nextDouble() * (axisAlignedBB.maxX - axisAlignedBB.minX);
                double posY = axisAlignedBB.minY + random.nextDouble() * (axisAlignedBB.maxY - axisAlignedBB.minY);
                double posZ = axisAlignedBB.minZ + random.nextDouble() * (axisAlignedBB.maxZ - axisAlignedBB.minZ);

                // 生成粒子
                entityPlayerMP.getServerWorld().spawnParticle(EnumParticleTypes.HEART, posX, posY, posZ,
                        1, // 粒子数量为1，因为我们在循环中每次生成一个粒子
                        0.0D, 0.0D, 0.0D, // 偏移量
                        0.0D); // 粒子速度
            }

            //取消技能持有状态
            playerData.updateSkillHandingAndSync(RangeHealth.skillName, false,entityPlayerMP);
            //技能设置为触发结束
            playerData.turnOffSkillExecuted(RangeHealth.skillName);
        }
    }
    public void handleOnPlayerTickRangeHealth(TickEvent.PlayerTickEvent event){
        PlayerData playerData = PlayerDataHandler.get(event.player);
        //检查技能执行状态
        if (playerData.checkSkillHanding(RangeHealth.skillName)) {
            EntityPlayerMP entityPlayerMP = (EntityPlayerMP) event.player;
            //技能持有时进行粒子渲染
            RangeHealth.handingRender(event, entityPlayerMP);
        }
    }
    public void handleOnTickPlayerManaHanding(TickEvent.PlayerTickEvent event){
        PlayerData data = PlayerDataHandler.get((EntityPlayer) event.player);
        if(data != null) {
            PlayerManaModifyHandler.INSTANCE.manaModifyTick(event.player,data.getManaRecover(),data);
        }
    }

    public void handleOnTickPlayerSelectedRangeClear(TickEvent.PlayerTickEvent event){
    }

    public void handleOnSkillRangeSelectedRender(float partialTicks){}
    //该方法设计为仅客户端触发
    public void handleOnPlayerSkillHandingOffAimBlock(PlayerInteractEvent.RightClickBlock event){}
    //该方法设计为仅客户端触发
    public void handleOnPlayerSkillHandingOffAimEmpty(PlayerInteractEvent.RightClickEmpty event){}

    //该方法设计为仅客户端触发
    public void handleOnMessageDataSync(MessageDataSync message){}





}
